Completed
Pull Request — master (#13)
by Justin
07:02
created

Fact.toJSON   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 3
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 1
dl 3
loc 3
rs 10
1 View Code Duplication
var Conclusion = require('./Conclusion'),
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
    PlaceReference = require('./PlaceReference'),
3
    GDate = require('./Date'),
4
    Qualifier = require('./Qualifier'),
5
    utils = require('./utils');
6
7
/**
8
 * A fact for a person or relationship.
9
 * 
10
 * @constructor
11
 * @param {Object} [json]
0 ignored issues
show
Documentation introduced by
The parameter [json] does not exist. Did you maybe forget to remove this comment?
Loading history...
12
 */
13
var Fact = function(json){
14
  
15
  // Protect against forgetting the new keyword when calling the constructor
16
  if(!(this instanceof Fact)){
17
    return new Fact(json);
18
  }
19
  
20
  // If the given object is already an instance then just return it. DON'T copy it.
21
  if(Fact.isInstance(json)){
22
    return json;
23
  }
24
  
25
  Conclusion.call(this, json);
26
  
27
  if(json){
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if json is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
28
    this.setType(json.type);
29
    this.setDate(json.date);
30
    this.setPlace(json.place);
31
    this.setValue(json.value);
32
    this.setQualifiers(json.qualifiers);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
33
  }
34
};
35
36
Fact.prototype = Object.create(Conclusion.prototype);
37
38
Fact._gedxClass = Fact.prototype._gedxClass = 'GedcomX.Fact';
39
40
/**
41
 * Check whether the given object is an instance of this class.
42
 * 
43
 * @param {Object} obj
44
 * @returns {Boolean}
45
 */
46
Fact.isInstance = function(obj){
47
  return utils.isInstance(obj, this._gedxClass);
48
};
49
50
/**
51
 * Get the fact type
52
 * 
53
 * @returns {String} type
54
 */
55
Fact.prototype.getType = function(){
56
  return this.type;
57
};
58
59
/**
60
 * Set the fact type
61
 * 
62
 * @param {String} type
63
 * @returns {Fact} This instance
64
 */
65
Fact.prototype.setType = function(type){
66
  this.type = type;
67
  return this;
68
};
69
70
/**
71
 * Get the date
72
 * 
73
 * @returns {Date} date
74
 */
75
Fact.prototype.getDate = function(){
76
  return this.date;
77
};
78
79
/**
80
 * Set the date
81
 * 
82
 * @param {Date|Object} date
83
 * @returns {Fact} This instance
84
 */
85
Fact.prototype.setDate = function(date){
86
  if(date){
87
    this.date = GDate(date);
88
  }
89
  return this;
90
};
91
92
/**
93
 * Get the place reference
94
 * 
95
 * @returns {PlaceReference}
96
 */
97
Fact.prototype.getPlace = function(){
98
  return this.place;
99
};
100
101
/**
102
 * Set the place reference
103
 *
104
 * @param {PlaceReference|Object} place
105
 * @returns {Fact} This instance
106
 */
107
Fact.prototype.setPlace = function(place){
108
  if(place){
109
    this.place = PlaceReference(place);
110
  }
111
  return this;
112
};
113
114
/**
115
 * Get the value
116
 * 
117
 * @returns {String}
118
 */
119
Fact.prototype.getValue = function(){
120
  return this.value;
121
};
122
123
/**
124
 * Set the value
125
 * 
126
 * @param {String} value
127
 * @returns {Fact} This instance
128
 */
129
Fact.prototype.setValue = function(value){
130
  this.value = value;
131
  return this;
132
};
133
134
/**
135
 * Get qualifiers
136
 * 
137
 * @return {Qualifer[]}
138
 */
139
Fact.prototype.getQualifiers = function(){
140
  return this.qualifiers || [];
141
};
142
143
/**
144
 * Set the qualifiers
145
 * 
146
 * @param {Qualifer[]|Object[]} qualifiers
147
 * @returns {Fact} This instance
148
 */
149
Fact.prototype.setQualifiers = function(qualifiers){
150
  if(Array.isArray(qualifiers)){
151
    this.qualifiers = [];
152
    var fact = this;
153
    qualifiers.forEach(function(q){
154
      fact.addQualifier(q);
155
    });
156
  }
157
};
158
159
/**
160
 * Add a qualifier
161
 * 
162
 * @param {Qualifier|Object} qualifier
163
 * @returns {Fact} This instance
164
 */
165
Fact.prototype.addQualifier = function(qualifier){
166
  if(qualifier){
167
    if(!Array.isArray(this.qualifiers)){
168
      this.qualifiers = [];
169
    }
170
    this.qualifiers.push(Qualifier(qualifier));
171
  }
172
  return this;
173
};
174
175
/**
176
 * Export the object as JSON
177
 * 
178
 * @return {Object} JSON object
179
 */
180
Fact.prototype.toJSON = function(){
181
  var json = Conclusion.prototype.toJSON.call(this);
182
  
183
  if(this.type){
184
    json.type = this.type;
185
  }
186
  
187
  if(this.date){
188
    json.date = this.date.toJSON();
189
  }
190
  
191
  if(this.place){
192
    json.place = this.place.toJSON();
193
  }
194
  
195
  if(this.value){
196
    json.value = this.value;
197
  }
198
  
199
  if(this.qualifiers){
200
    json.qualifiers = this.qualifiers.map(function(q){
201
      return q.toJSON();
202
    });
203
  }
204
  
205
  return json;
206
};
207
208
module.exports = Fact;